﻿using PixivTranslate.Utils.IoUtils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace PixivTranslate.Utils.Currency.IoUtils
{
    internal class FloderNameHelper
    {


        /// <summary>
        /// 获取文件夹路径  以\\结尾
        /// </summary>
        /// <param name="Floderpath"></param>
        /// <returns></returns>
        public string GetFloderPath(String Floderpath)
        {
        
            try { Floderpath = Path.GetDirectoryName(Floderpath) + "\\"; }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return Floderpath;
        }
        /// <summary>
        /// 获取文件夹名字 例如：c://cat// 返回cat
        /// </summary>
        /// <param name="Floderpath"></param>
        /// <returns></returns>
        public string GetFloderName(string Floderpath)
        {


            try
            {
                Floderpath = GetFloderPath(Floderpath);
                Floderpath = Floderpath.Substring(0, Floderpath.Length - 1);
            }
            catch (Exception ex)
            {
                Console.WriteLine("获取文件名不正确");
            }
            return new FileNameHelper().GetFileName(Floderpath);
        }
        /// <summary>
        /// 创建一个文件夹路径
        /// </summary>
        /// <param name="ParentFloder">父路径，以\\结尾</param>
        /// <param name="ChildFloderName">子路径，不需要\\，直接写“FloderName”即可</param>
        /// <param name="CreateFlag">是否创建文件夹</param>
        /// <returns></returns>
        public string CreateFloderPath(string ParentFloder, string ChildFloderName, bool CreateFlag = true)
        {
            if (!(ParentFloder.LastIndexOf("\\") == ParentFloder.Length - 1)) {
                //如果不以\\结尾，则代表这个路径没有封闭，先进行封闭
                System.Console.WriteLine("父路径未封闭，请注意以\\进行结尾");
                ParentFloder = ParentFloder + "\\";
            }
            ParentFloder = ParentFloder + TextHelper.ReplaceBadChar(ChildFloderName) + "\\";
            if (CreateFlag)
                new FloderHelper().FolderCreate(ParentFloder);

            return ParentFloder;
        }

        /// <summary>
        /// 获取目录下文件列表，不带路径
        /// </summary>
        /// <param name="FloderPath"></param>
        /// <param name="Pattern"></param>
        /// <returns></returns>
        public List<String> GetFileNameList(String FloderPath, String Pattern = "*.*")
        {
            List<String> FileList = GetFullFileNameList(FloderPath, Pattern);
            List<string> TempList = new List<string>();
            foreach (string Line in FileList)
            {

                TempList.Add(new FileNameHelper().GetFileName(Line));
            }
            return TempList;
        }

        /// <summary>
        /// 获取目录下文件列表，带路径
        /// </summary>
        /// <param name="FloderPath"></param>
        /// <param name="Pattern"></param>
        /// <returns></returns>
        public  List<String> GetFullFileNameList(String FloderPath, String Pattern = "*.*")
        {
            List<String> FileList = new List<String>();
            try
            {
                FileList = Directory.GetFiles(FloderPath, Pattern).ToList<String>();
            }
            catch (Exception ex)
            {
                PrintLog.Log("获取文件列表失败，文件夹{0}没有创建。", FloderPath);
                PrintLog.Log(ex);
            }
            return FileList;
        }

        /// <summary>
        /// 获取文件夹列表
        /// </summary>
        /// <param name="FloderPath"></param>
        /// <returns></returns>
        public  List<string> GetFloderNameList(string FloderPath)
        {
            List<string> FloderList = new List<string>();
            if (!new FloderHelper(). FloderExits(FloderPath, true))
            {
                Console.WriteLine("创建新文件夹失败");
                return FloderList;
            }
            try
            {
                foreach (string SingleFloder in Directory.GetDirectories(FloderPath).ToList<string>())
                {
                    FloderList.Add(SingleFloder + "\\");
                }
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return FloderList;
        }

    }
}